package com.bahiaapk;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.util.Linkify;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bahiaapk.dto.TwitterMessage;
import com.bahiaapk.util.BahiaUtil;
import com.raptureinvenice.webimageview.image.WebImageView;

public class TwitterDetailActivity extends Activity{

	
	private TextView titleTX;
	private TextView dateTX;
	private TextView titleTopTX;
	private TextView messageTX;

	private RelativeLayout layoutWebview;
	private Display display;
	private Integer heightScreen;
	private WebImageView webImageView;

	private TwitterMessage twitterMessage;
	  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.twitter_detail);
        
        layoutWebview = (RelativeLayout) findViewById(R.id.layout_webview);
        titleTX= (TextView) findViewById(R.id.titleTX);
        dateTX= (TextView) findViewById(R.id.dateTX);
        webImageView= (WebImageView) findViewById(R.id.image);
        titleTopTX = (TextView) findViewById(R.id.titleTopTX);
        messageTX  = (TextView) findViewById(R.id.messageTX);

        fixScreenSize();
        
        Bundle loadInfo = getIntent().getExtras();
        twitterMessage = (TwitterMessage) loadInfo.getSerializable("twitterMessage");

        detail();       
      
    }


	private void fixScreenSize() {
		display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
		
        heightScreen = display.getHeight();
        int newValue = (int) (heightScreen*0.68);
        
        layoutWebview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, newValue));
	}
	
	

	private void detail() {
		
		titleTopTX.setText("Twitter News");		
		titleTX.setText(twitterMessage.getName());
		dateTX.setText(twitterMessage.getCreatedAtFormat());
		webImageView.setImageWithURL(this,twitterMessage.getUrlImage());
		
		messageTX.setText(twitterMessage.getMessage());
		Linkify.addLinks(messageTX, Linkify.ALL);
		
	}
	
	public void sendEmail(View view){
		if(BahiaUtil.haveNetworkConnection(this)){
			String url = "<a href=http://www.twitter.com/"+twitterMessage.getName()+">"+twitterMessage.getName()+"</a>";
			Intent gmail = new Intent(Intent.ACTION_VIEW);
			gmail.setComponent(new ComponentName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail"));
		    gmail.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
		    gmail.putExtra(Intent.EXTRA_SUBJECT, "BAHIAPK: "+"News @"+twitterMessage.getName());
		    gmail.setType("plain/text");
		    gmail.putExtra(Intent.EXTRA_TEXT, Html.fromHtml( url+"<br/><br/>"+" "+twitterMessage.getCreatedAtFormat()+"<br/><br/><br/><br/>"+twitterMessage.getMessage()+"<br/><br/><br/><br/>"));

		    try {
		    	startActivity(gmail);
		    } catch (android.content.ActivityNotFoundException ex) {
				return;
		    }
		    return;
 		}else{
			Toast.makeText(this, "Internet offline", Toast.LENGTH_LONG).show();
		}
		
		
	}
	 public void openTwitterShare(View view){
		 if(BahiaUtil.haveNetworkConnection(this)){
			 DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
				    @Override
				    public void onClick(DialogInterface dialog, int which) {
				        switch (which){
				        case DialogInterface.BUTTON_POSITIVE:
				        	Intent intent = new Intent(TwitterDetailActivity.this, RetweetActivity.class);//ServiceSelectActivity.class);
							intent.putExtra("twitterMessage", twitterMessage);
							startActivity(intent);
				            break;
	
				        case DialogInterface.BUTTON_NEGATIVE:
				            //No button clicked
				            break;
				        }
				    }
				};
	
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setMessage("Deseja Retwitar essa messagem?").setPositiveButton("Sim", dialogClickListener)
				    .setNegativeButton("Nao", dialogClickListener).show();
		 }else{
			Toast.makeText(this, "Internet offline", Toast.LENGTH_LONG).show();
		 }
			
		 
		 
	    
	    }
	public void sendFacebook(View view){


	}

}
